package com.frost.book.controller;

import com.frost.book.constant.Constant;
import com.frost.book.model.*;
import com.frost.book.service.BookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j
@RequestMapping("/book")
@RestController
//@ResponseBody
//@Component
//@Controller
public class BookController {

    @Autowired
    private BookService bookService;

//    @RequestMapping("/getList")
//    public List<BookInfo> getList() {
//        // 1.从数据库中获取数据
//        // 数据采用Mock的方式：采用虚假的数据进行
//        List<BookInfo> bookInfos = bookService.getBookList();
//
//        // 3.返回数据
//        return bookInfos;
//    }

    @RequestMapping(value = "/addBook", produces = "application/json")
    public Result addBook(BookInfo bookInfo) {
        log.info("添加图书, bookInfo:{}", bookInfo);
        if (!StringUtils.hasLength(bookInfo.getBookName())
                || !StringUtils.hasLength(bookInfo.getAuthor())
                || bookInfo.getCount() <= 0
                || bookInfo.getPrice() == null
                || !StringUtils.hasLength(bookInfo.getPublish())) {
//            return "参数错误";
            return Result.fail("参数错误");
        }
        try {
            bookService.insertBook(bookInfo);
        } catch (Exception e) {
            log.error("添加图书失败, e:{}", e);
//            return "内部发生错误";
            return Result.fail("内部发生错误");
        }
        return Result.success("");
    }

    @RequestMapping("/getListByPage")
    public Result<PageResult<BookInfo>> getListByPage(PageRequest pageRequest, HttpSession session) {
        log.info("查询列表信息: pageRequest:{}", pageRequest);
        // 验证用户是否登录
        // 进行 debug 看看 session中有什么参数
//        UserInfo userInfo = (UserInfo) session.getAttribute(Constant.USER_SESSION_KEY);
//        if (userInfo == null || userInfo.getId() < 1) {
//            return Result.unlogin();
//        }

        if (pageRequest.getPageSize() < 1) {
            return Result.fail("非法参数");
        }

        PageResult<BookInfo> pageResult = bookService.getListByPage(pageRequest);
        return Result.success(pageResult);
    }

    @RequestMapping("/queryBookById")
    public BookInfo queryBookById(Integer bookId) {
        return bookService.queryBookById(bookId);
    }

    @RequestMapping("/updateBook")
    public Result updateBook(BookInfo bookInfo) {
        log.info("更新图书, bookInfo:{}", bookInfo);
        if (bookInfo.getId() < 0) {
            return Result.fail(false, "id 不合法");
        }
        try {
            Integer result = bookService.updateBook(bookInfo);
            if (result < 1) {
                return Result.fail(false, "更新失败");
            }
            return Result.success(true);
        } catch (Exception e) {
            log.error("更新图书失败, e:{}", e);
            return Result.fail(false, e.getMessage());
        }
    }

    /**
     * 批量删除图书
     * @param ids
     * @return
     */
    @RequestMapping("/batchDelete")
    public Boolean batchDelete(@RequestParam List<Integer> ids) {
        log.info("批量删除图书, ids:{}", ids);
        try {
            Integer result = bookService.batchDelete(ids);
            return result > 0;
        } catch (Exception e) {
            log.error("批量删除失败, ids:{}, e:{}", ids, e);
            return false;
        }
    }
}
